package com.zoroki.hmusic.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zoroki.hmusic.constant.MessageConstant;
import com.zoroki.hmusic.entity.Result;
import com.zoroki.hmusic.entity.Song;
import com.zoroki.hmusic.service.ISongService;
import com.zoroki.hmusic.util.QiniuUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zoroki
 * @since 2023-05-10
 */
@RestController
@RequestMapping("/song")
@CrossOrigin("*")
@Tag(name = "歌曲")
public class SongController {

    @Autowired
    private ISongService songService;

    @Operation(summary = "添加歌曲")
    @PostMapping("/addSong")
    @ResponseBody
    public Result addSong(@RequestBody Song song) {
        int i = songService.addSong(song);
        if (i > 0) {
            return new Result(true, MessageConstant.SONG_ADD_SUCCESS, null);
        }
        return new Result(false, MessageConstant.SONG_ADD_FAILURE, null);
    }

    @Operation(summary = "根据用户Id获取歌曲列表")
    @GetMapping("/getSongList")
    @ResponseBody
    public Result getSongList(@RequestParam int userId) {
        List<Song> songList = songService.getSongList(userId);
        if (songList.size() > 0) {
            return new Result(true, MessageConstant.SONG_GET_SUCCESS, songList);
        }
        return new Result(false, MessageConstant.SONG_GET_FAILURE, null);
    }

    @Operation(summary = "分页查询用户上传的歌曲")
    @GetMapping("/getSongPage")
    @ResponseBody
    public Result getSongPage(@RequestParam int userId, @RequestParam int currentPage, @RequestParam int pageSize) {

        Page<Song> songPage = songService.getSongPage(userId, currentPage, pageSize);
        if (songPage.getRecords().size() > 0) {
            return new Result(true, MessageConstant.SONG_GET_SUCCESS, songPage);
        }
        return new Result(false, MessageConstant.SONG_GET_FAILURE, songPage);
    }

    @Operation(summary = "根据id和歌曲文件名删除歌曲")
    @DeleteMapping("/deleteSong")
    @ResponseBody
    public Result deleteSong(@RequestParam int id, @RequestParam String imgName) {
        int i = songService.deleteSong(id);
        if (i > 0) {
            QiniuUtil.deleteFile(MessageConstant.FOLDER_MUSIC + imgName);
            return new Result(true, MessageConstant.SONG_DELETE_SUCCESS, null);
        }

        return new Result(false, MessageConstant.SONG_DELETE_FAILURE, null);
    }

    @Operation(summary = "根据id查询歌曲")
    @GetMapping("/findSongById")
    @ResponseBody
    public Result findSongById(@RequestParam int id) {
        Song song = songService.findSongById(id);
        if (song != null) {
            return new Result(true, MessageConstant.YES, song);
        }
        return new Result(false, MessageConstant.NO, null);
    }

    @Operation(summary = "根据收藏Id查找歌曲")
    @GetMapping("/findSongByPlaylistId")
    @ResponseBody
    public Result findSongByPlaylistId(@RequestParam int playlistId) {

        List<Song> songs = songService.findSongByPlaylistId(playlistId);

        if (songs.size() > 0) {
            return new Result(true, MessageConstant.YES, songs);
        }
        return new Result(false, MessageConstant.NO, null);
    }

    @Operation(summary = "更新歌曲")
    @PutMapping("/updateSong")
    @ResponseBody
    public Result updateSong(@RequestBody Song song) {
        Song songRes = songService.updateSong(song);
        if (songRes!=null) {
            return new Result(true, MessageConstant.YES, songRes);
        }
        return new Result(false, MessageConstant.NO, null);
    }

    @Operation(summary = "根据keyword查找歌曲")
    @GetMapping("/findSongByKeyword")
    @ResponseBody
    public Result findSongByKeyword(@RequestParam String keyword) {

        SearchHits<Song> songs = songService.findSongByKeyword(keyword);
        if (songs.getTotalHits() > 0) {
            return new Result(true, MessageConstant.YES, songs);
        }
        return new Result(false, MessageConstant.NO, null);
    }

    @Operation(summary = "根据keyword查找歌曲分页")
    @GetMapping("/getSongPageByKeyword")
    @ResponseBody
    public Result getSongPageByKeyword(@RequestParam String keyword,@RequestParam int pages) {
        org.springframework.data.domain.Page<Song> songs = songService.getSongPageByKeyword(keyword,pages);
        if (songs.getTotalElements() > 0) {
            return new Result(true, MessageConstant.YES, songs);
        }
        return new Result(false, MessageConstant.NO, null);
    }

    @Operation(summary = "根据number在es中查询新上传的歌曲数量")
    @GetMapping("/getNewSongFromEs")
    @ResponseBody
    public Result getNewSongFromEs(@RequestParam int number) {
        org.springframework.data.domain.Page<Song> songs = songService.getNewSongFromEs(number);
        if (songs.getTotalElements() > 0) {
            return new Result(true, MessageConstant.YES, songs);
        }
        return new Result(false, MessageConstant.NO, null);
    }
}
